<?php
session_start();
require('../conn.php');
require('../functions.php');
require('admin.php');
$page = $_GET['page'];
if (is_numeric($page) == false) {
    $page = 1;
    $page_1 = 1;
    $min = 0;
    $page_2 = $page + 1;
} else {
    if ($page > 1) {
        $page_2 = $page + 1;
        $min = ($page - 1) * 35;
        $page_1 = $page - 1;
    } else {
        $page_1 = 1;
        $min = 0;
        $page_2 = 2;
    }
}
$max = $page * 35;
$sql = "select * from `article` where `top` > -2  order by `id` desc limit {$min},{$max}";
if ($_POST) {
    $chkArr = $_POST['checkbox'];
    if ($_POST['do'] == 'state_top' && is_array($chkArr)) {
        foreach ($chkArr as $k => $v) {
            if (is_numeric($v) == false) {
                continue;
            }
            $mysql->execute("update `article` set  `top` =  '1' where `id`='{$v}'");
        }
        echo '<script>alert(\'置顶成功\');location.href=\'article.php\'</script>';
        exit;
    }
    if ($_POST['do'] == 'state_pub' && is_array($chkArr)) {
        foreach ($chkArr as $k => $v) {
            if (is_numeric($v) == false) {
                continue;
            }

            $aids=$aids.",".$v;
            $mysql->execute("update `article` set  `top` =  0 where `id`='{$v}'");
        }

        $aids=substr($aids, 1);

        $rows=$mysql->query("select id,day,type,code from `article` where `id` in({$aids}) ");
        // var_dump($rows);
        // die();
        $a=0;
        ob_start();
        foreach ($rows as $row) {
            $a++;



            $aid=$row['id'];
            $atype=$row['type'];
            $acode=$row['code'];

            $url="http://localhost/weiyue_houtai/detail.php?aid=$aid";

            do{
                $content = get_contents($url);
            }while(!strpos($content,'html'));

            if (!file_exists("../html/".$row['day'])){
                mkdir ("../html/".$row['day']);
            }

            $filename="../html/".$row['day']."/1".($aid+12345).$atype.$acode.".html";
            $fp = fopen($filename, "w");
            fwrite($fp, $content);
            fclose($fp);

        }
        ob_end_clean();
        ob_flush();

        echo '<script>alert(\'生成成功\');location.href=\'article.php\'</script>';

        exit;
    }
    if ($_POST['do'] == 'state_pre' && is_array($chkArr)) {
        foreach ($chkArr as $k => $v) {
            if (is_numeric($v) == false) {
                continue;
            }
            $mysql->execute("update `article` set  `top` =  -1 where `id`='{$v}'");
        }
        echo '<script>alert(\'待发布成功\');location.href=\'article.php\'</script>';
        exit;
    }
    if ($_POST['do'] == 'state_del' && is_array($chkArr)) {
        foreach ($chkArr as $k => $v) {
            if (is_numeric($v) == false) {
                continue;
            }
            $mysql->execute("update `article` set  `top` =  -2 where `id`='{$v}'");
        }
        echo '<script>alert(\'删除成功\');location.href=\'article.php\'</script>';
        exit;
    }
}
$update_id = guolv($_GET['update_id']);
$state = guolv($_GET['state']);
if (is_numeric($update_id) && is_numeric($state)) {
    $mysql->query("update `article` set  `top` = {$state}  where `id`= '{$update_id}' and `top`>-2");

    if ($state==0) {

        $row=$mysql->query("select id,day,type,code from `article` where `id`= '{$update_id}' ");

        ob_start();

        // $url={$site}"/detail.php?aid=421";
        $url="http://localhost/weiyue_houtai/detail.php?aid=$update_id";
        // $url={$site}/detail.php?aid={$v_article['id']}
        do{
            $content = get_contents($url);
        }while(!strpos($content,'html'));

        if (!file_exists("../html/".$row[0]['day'])){
            mkdir ("../html/".$row[0]['day']);
        }

        // $filename="../html/".$row[0]['day']."/1".($row[0]['id']+12345.$row[0]['type']).".html";
        $filename="../html/".$row[0]['day']."/1".($row[0]['id']+12345).($row[0]['type']).($row[0]['code']).".html";
        $fp = fopen($filename, "w");
        fwrite($fp, $content);
        fclose($fp);
        ob_end_clean();
        ob_flush();

    }

    echo '<script>alert(\'操作成功\');location.href=\'article.php\'</script>';
    exit;
}
if ($_GET['do'] == 'search') {
    $search_title = guolv($_GET['search_title']);
    $sql = "select * from `article` where `top` > -2 AND `title` LIKE  '%{$search_title}%' order by `id` desc limit 35";
}
if ($_GET['order'] == 'pv') {
    $sql = "select * from `article` where `top` > -2 order by -`pv` limit 0,100";
}

$search_day = guolv($_GET['day']);
if (preg_match('/\d{4}-1[0-2]|0?[1-9]-0?[1-9]|[12][0-9]|3[01]/', $search_day)) {
    $sql = "select * from `article` where `top` > -2 AND `day`='{$search_day}'";
}
$search_state = guolv($_GET['search_state']);
if (is_numeric($search_state)) {
    $sql = "select * from `article` where `top` > -2 AND `top`='{$search_state}'";
}
$row = $mysql->query($sql);
?>

<?php include('head.php') ?>
<script type="text/javascript">
    function check_all(obj, cName) {
        var checkboxs = document.getElementsByName(cName);
        for (var i = 0; i < checkboxs.length; i++) {
            checkboxs[i].checked = obj.checked;
        }
    }
</script>
<div class="container-fluid">

    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span9">
            <h1 class="page-title">轮播管理</h1>
            <!--
            <div class="btn-toolbar">
                <a href="#myModal" data-toggle="modal" class="btn">Delete</a>
              <div class="btn-group">
              </div>
            </div>
            -->
            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <div class="well">
                            <form class="navbar-search pull-left" action="article.php" method="get">
                                <input type="hidden" name="do" value="search">
                                <input type="text" class="search-query" placeholder="搜索文章标题+回车" name="search_title">
                            </form>
                            <div class="btn-group pull-right">
                                <div class="btn-group">
                                    <button class="btn dropdown-toggle" data-toggle="dropdown">
                                        选择板块<span class="caret"/>
                                    </button >
                                    <ul class="dropdown-menu">
                                        <li><a href="article.php">文章管理</a></li>
                                        <li><a href="picture.php">图片管理</a></li>
                                        <li><a href="video.php">视频管理</a></li>
                                        <li><a href="jpush.php">推送管理</a></li>
                                        <li><a href="lunbo.php">轮播管理</a></li>
                                        <li><a href="tuijian.php">推荐管理</a></li>
                                    </ul>
                                </div>
                            </div>
                            <div class="btn-group pull-right">
                                <div class="btn-group">
                                    <button class="btn dropdown-toggle" data-toggle="dropdown">
                                        选择状态<span class="caret"/>
                                    </button >
                                    <ul class="dropdown-menu">
                                        <?php
                                        for($i=2;$i>=-1;$i--){
                                            switch ($i) {
                                                case '1': {
                                                    echo "<li><a href='?search_state={$i}' style=\"color:red\">置顶</a></li>";
                                                    break;
                                                }
                                                case '0': {
                                                    echo "<li><a href='?search_state={$i}' style=\"color:green\">已发布</a></li>";
                                                    break;
                                                }
                                                case '-1': {
                                                    echo "<li><a href='?search_state={$i}' style=\"color:blue\">待发布</a></li>";
                                                    break;
                                                }
//                                                case '-2': {
//                                                    echo "<li><a href='?search_state={$i}' style=\"color:darkgrey\">已删除</a></li>";
//                                                    break;
//                                                }
                                                default : {
                                                    echo "<li><a href='?1=1' style=\"color:black\">所有状态</a></li>";
                                                    break;
                                                }
                                            }

                                        }
                                        ?>
                                    </ul>
                                </div>
                                <div class="btn-group">
                                    <button class="btn dropdown-toggle" data-toggle="dropdown">
                                        选择日期<span class="caret"/>
                                    </button >
                                    <ul class="dropdown-menu">
                                        <?php
                                        echo "<li><a href='?1=1'>所有日期</a></li>";
                                        for ($i = 0; $i <= 30; $i++) {
                                            $day = date('Y-m-d', strtotime('-' . $i . ' day'));
                                            echo "<li><a href='?day={$day}'>{$day}</a></li>";
                                        } ?>
                                    </ul>
                                </div>
                                <a href="article.php" class="btn" style="margin-left:10px;">首页</a></button>
                                <a href="article.php?page=<?php echo $page_1 ?>" class="btn"
                                   style="margin-left:5px;">上一页</a>
                                <a href="article.php?page=<?php echo $page_2 ?>" class="btn"
                                   style="margin-left:5px;">下一页</a>
                            </div>
                            <table class="table">
                                <form class="navbar-search pull-left" action="article.php" method="post">
                                    <thead>
                                    <tr>
                                        <th></th>
                                        <th>发布日期</th>
                                        <th>标题</th>
                                        <th><a href="?order=pv">有效阅读</a></th>
                                        <th>扩展</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    <?php

                                    $sql="select * from `article` where flag=1 order by `id` desc";
//                                    $row = $mysql->query($sql);
                                    foreach ($row as $v_article) {
                                        $pic = str_replace('[weixin]', $image_protect, $v_article['pic']);
                                        $article_title = htmlspecialchars($v_article['title']);
                                        $top = $v_article['top'];
                                        switch ($top) {
                                            case 1: {
                                                $top = "<font color=red>[置顶]</font>";
                                                $btn = "
<a class=\"btn\" style=\"color:green\" onclick=\"update_id({$v_article['id']}, 0)\" title=\"已发布\">已发布</a>
<a class=\"btn\" style=\"color:blue\" onclick=\"update_id({$v_article['id']}, -1)\" title=\"待发布\">待发布</a>
<a class=\"btn\" style=\"color:darkgrey\" onclick=\"update_id({$v_article['id']}, -2)\" title=\"删除\">已删除</a>";
                                                break;
                                            }
                                            case 0: {
                                                $top = "<font color=green>[已发布]</font>";
                                                $btn = "
<a class=\"btn\" style=\"color:red\" onclick=\"update_id({$v_article['id']}, 1)\" title=\"置顶\">置顶</a>
<a class=\"btn\" style=\"color:blue\" onclick=\"update_id({$v_article['id']}, -1)\" title=\"待发布\">待发布</a>
<a class=\"btn\" style=\"color:darkgrey\" onclick=\"update_id({$v_article['id']}, -2)\" title=\"删除\">已删除</a>";
                                                break;
                                            }
                                            case -1: {
                                                $top = "<font color=blue>[待发布]</font>";
                                                $btn = "
<a class=\"btn\" style=\"color:red\" onclick=\"update_id({$v_article['id']}, 1)\" title=\"置顶\">置顶</a>
<a class=\"btn\" style=\"color:green\" onclick=\"update_id({$v_article['id']}, 0)\" title=\"已发布\">已发布</a>
<a class=\"btn\" style=\"color:darkgrey\" onclick=\"update_id({$v_article['id']}, -2)\" title=\"删除\">已删除</a>";
                                                break;
                                            }
                                            case -2: {
                                                $top = "<font color=#a9a9a9>[已删除]</font>";
                                                $btn = "
<a class=\"btn\" style=\"color:red\" onclick=\"update_id({$v_article['id']}, 1)\" title=\"置顶\">置顶</a>
<a class=\"btn\" style=\"color:green\" onclick=\"update_id({$v_article['id']}, 0)\" title=\"已发布\">已发布</a>
<a class=\"btn\" style=\"color:blue\" onclick=\"update_id({$v_article['id']}, -1)\" title=\"待发布\">待发布</a>";
                                                break;
                                            }
                                        }
                                        print <<<table
        <tr>
		  <td><input type="checkbox" name="checkbox[]" value="{$v_article['id']}"> </td>
          <td>{$v_article['day']}</td>
          <td>{$top} <a class="btn btn btn-small" target="_blank" href="{$pic}" target="_blank" title="缩略图查看"><i class="icon-picture"></i></a> <a href="{$site}/detail.php?aid={$v_article['id']}" target="_blank">{$article_title}</a></td>
		  <td>{$v_article['pv']}</td>
		  <td>{$v_article['extra']}</td>
		  <td>
			<font color=grey>￥{$v_article['money']}</font>
			<a class="btn btn btn-small" target="_blank" href="edit_article.php?id={$v_article['id']}" title="修改"><i class="icon-edit"></i></a>
			{$btn}
		  </td>
        </tr>
table;
                                    } ?>
                                    </tbody>
                            </table>
                            <div>
                                <p>
                                    <span><input type="checkbox" name="all" onclick="check_all(this,'checkbox[]')"/> 全选 / 全不选</span>
	<span style="padding-left:20px;">
	批量操作：
	<select name="do" style="width:100px;">
        <option value="state_top">置顶</option>
        <option value="state_pub">已发布</option>
        <option value="state_pre">待发布</option>
        <option value="state_del">已删除</option>
    </select>
	<input type="submit" class="btn btn btn-small" value="提交">
	</span>
                                </p>
                            </div>
                        </div>
                        </form>
                    </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>

</div>

<footer>
    <hr>

    <p class="pull-right"><!--power by right--></p>


    <p><!--power by --></p>
</footer>


<!-- Le javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="lib/bootstrap/js/bootstrap.js"></script>
<script language="JavaScript">
    function del_id(id) {
        if (confirm("删除是不可恢复的，你确认要删除吗？")) {
            location.href = "?del_id=" + id;

        } else {
            return false;
        }
    }
    function update_id(id, state) {
        var str = "";
        switch (state) {
            case -2: {
                str = " [已删除] ";
                break;
            }
            case -1: {
                str = " [待发布] ";
                break;
            }
            case 0: {
                str = " [已发布] ";
                break;
            }
            case 1: {
                str = " [置顶] ";
                break;
            }
        }
        if (confirm("状态更新为" + str + "，确定操作？")) {
            location.href = "?update_id=" + id + "&state=" + state;
        } else {
            return false;
        }
    }
</script>


</body>
</html>